// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProfileFacade.cs" company="">
//   
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace ExecutionTool.Profile
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Xml;

    public static class ProfileFacade
    {
        #region Constants

        private const string ProfileDataFileName = "SqlAzureDataProtector.profile";

        #endregion

        #region Public Methods and Operators

        public static Profile LoadProfile(string profileName)
        {
            var profiles = LoadProfiles();
            var profile = profiles.FirstOrDefault(p => p.Name.Equals(profileName, StringComparison.InvariantCultureIgnoreCase));

            if (profile == null)
            {
                throw new ArgumentException(string.Format("The specified profile name [{0}] doesn't exist.", profileName));
            }

            return profile;
        }

        public static List<Profile> LoadProfiles()
        {
            if (File.Exists(ProfileDataFileName))
            {
                using (var textReader = new StreamReader(ProfileDataFileName))
                {
                    var serializer = new DataContractSerializer(typeof(List<Profile>));

                    using (var reader = XmlReader.Create(textReader))
                    {
                        var profiles = serializer.ReadObject(reader) as List<Profile>;

                        if (profiles == null)
                        {
                            return new List<Profile>();
                        }

                        foreach (var profile in profiles)
                        {
                            profile.Validate();
                        }

                        return profiles;
                    }
                }
            }

            return new List<Profile>();
        }

        public static bool ProfileDataExists()
        {
            return File.Exists(ProfileDataFileName);
        }

        public static void SaveProfiles(List<Profile> profiles)
        {
            if (profiles == null)
            {
                return;
            }

            foreach (var profile in profiles)
            {
                profile.Validate();
            }

            var serializer = new DataContractSerializer(typeof(List<Profile>));

            using (var textWriter = new StreamWriter(ProfileDataFileName, false))
            {
                using (var writer = XmlWriter.Create(textWriter, new XmlWriterSettings { Indent = true, IndentChars = "\t" }))
                {
                    serializer.WriteObject(writer, profiles);
                }
            }
        }

        #endregion
    }
}
